import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/home/home.vue'

Vue.use(Router)

const whiteList = ['/login', '/register', '/forgotPassword'];

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: { showTabbar: true }
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { showTabbar: true }
    }, {
      path: '/task',
      component: () => import('@/views/task/daiban.vue'),
      meta: { showTabbar: true }
    }
    ,
    {
      path: '/message',
      component: () => import('@/views/message/message.vue'),
      meta: { showTabbar: true }
    },
    {
      path: '/messageDetail',
      component: () => import('@/views/message/messageDetail.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/contacts',
      component: () => import('@/views/contacts/contacts.vue'),
      meta: { showTabbar: true }
    },
    // {
    //   path: '/contactDetail',
    //   component: () => import('@/views/contacts/contactDetail.vue'),
    //   meta: { showTabbar: false }
    // },
    {
      path: '/contactDetail',
      component: () => import('@/views/contacts/UserProfile.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/my',
      component: () => import('@/views/my/my.vue'),
      meta: { showTabbar: true }
    },
    {
      path: '/flowDetail',
      name: 'FlowDetail',
      component: () => import('@/views/component/flowDetail.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/forgotPassword',
      name: 'ForgotPassword',
      component: () => import('@/views/forgotPassword.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/register.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/flowCreate',
      name: 'FlowCreate',
      component: () => import('@/views/component/flowCreate.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/userSelect',
      name: 'UserSelect',
      component: () => import('@/views/userSelect.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/chat',
      name: 'Chat',
      component: () => import('@/views/message/chat.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/chatList',
      name: 'ChatList',
      component: () => import('@/views/chatList.vue'),
      meta: { showTabbar: false }
    },
    {
      path: '/userProfile',
      name: 'UserProfile',
      component: () => import('@/views/contacts/userProfile.vue'),
      meta: { showTabbar: false }
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (whiteList.includes(to.path)) {
    next();
  } else {
    if (token) {
      next();
    } else {
      next({ path: '/login' });
    }
  }
});

export default router;
